//
//  UIFont+ZHFont.m
//  differentIPhoneWithDifferentFont
//
//  Created by bigarto-mac1 on 2018/3/6.
//  Copyright © 2018年 bigarto-mac1. All rights reserved.
//

#import "UIFont+ZHFont.h"
#import <objc/runtime.h>

@implementation NSObject (Extension)

+ (void)exchangeClassSelector:(SEL)originalSelector withNewClassSelector:(SEL)newSelector {
    Method originalMethod = class_getClassMethod(self, originalSelector);
    Method newMethod = class_getClassMethod(self, newSelector);
    method_exchangeImplementations(originalMethod, newMethod);
}

@end

@implementation UIFont (ZHFont)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self exchangeClassSelector:@selector(systemFontOfSize:) withNewClassSelector:@selector(zh_systemFontOfSize:)];
        [self exchangeClassSelector:@selector(systemFontOfSize:weight:) withNewClassSelector:@selector(zh_systemFontOfSize:weight:)];
        [self exchangeClassSelector:@selector(boldSystemFontOfSize:) withNewClassSelector:@selector(zh_boldSystemFontOfSize:)];
        [self exchangeClassSelector:@selector(italicSystemFontOfSize:) withNewClassSelector:@selector(zh_italicSystemFontOfSize:)];
        [self exchangeClassSelector:@selector(fontWithName:size:) withNewClassSelector:@selector(zh_fontWithName:size:)];
    });
}

+ (CGFloat)getDifferentDeviceFontSize:(CGFloat)fontSize {
    CGFloat fontScale = 1.0;
    if (kDevice_Is_iPhone4 || kDevice_Is_iPhone5) {
        fontScale = 0.8;
    } else if (kDevice_Is_iPhone6) {
        fontScale = 1.0;
    } else if (kDevice_Is_iPhone6Plus) {
        fontScale = 1.2;
    } else if (kDevice_Is_iPhone6Plus_BigMode || kDevice_Is_iPhoneX) {
        fontScale = 1.1;
    } else {
        fontScale = 0.7;
    }
    return fontSize * fontScale;
}

+ (UIFont *)zh_systemFontOfSize:(CGFloat)fontSize {
    return [self zh_systemFontOfSize:[self getDifferentDeviceFontSize:fontSize]];
}

+ (UIFont *)zh_systemFontOfSize:(CGFloat)fontSize weight:(CGFloat)weight {
    return [self zh_systemFontOfSize:[self getDifferentDeviceFontSize:fontSize] weight:weight];
}

+ (UIFont *)zh_boldSystemFontOfSize:(CGFloat)fontSize {
    return [self zh_boldSystemFontOfSize:[self getDifferentDeviceFontSize:fontSize]];
}

+ (UIFont *)zh_italicSystemFontOfSize:(CGFloat)fontSize {
    return [self zh_italicSystemFontOfSize:[self getDifferentDeviceFontSize:fontSize]];
}

+ (nullable UIFont *)zh_fontWithName:(NSString *)fontName size:(CGFloat)fontSize {
    return [self zh_fontWithName:fontName size:[self getDifferentDeviceFontSize:fontSize]];
}

@end
